#!/bin/sh

OLDPATH="$PATH"

cleanup() {
  PATH="$OLDPATH"
  unset -f wget
  case "${TEST_DIR-}" in
    /*) rm -rf "${TEST_DIR}" ;;
  esac
}
die () { echo "$@" ; cleanup ; exit 1; }

: nvm.sh
\. ../../../nvm.sh

TEST_DIR="$(mktemp -d)" || die 'unable to create a temporary directory'
mkdir "${TEST_DIR}/bin" || die 'unable to create the fake bin directory'

wget() {
  : > "${TEST_DIR}/function"
  return 97
}

cat > "${TEST_DIR}/bin/wget" <<EOF
#!/bin/sh
: > "${TEST_DIR}/args"
for arg in "\$@"; do
  printf '%s|\n' "\$arg" >> "${TEST_DIR}/args"
done
cat > "${TEST_DIR}/stdin"
printf 'OUT\n'
printf 'ERR\n' >&2
exit "\$(cat "${TEST_DIR}/exit")"
EOF
chmod a+x "${TEST_DIR}/bin/wget" || die 'unable to make the fake wget executable'
PATH="${TEST_DIR}/bin:${PATH}"

reset_logs() {
  rm -f "${TEST_DIR}/args" "${TEST_DIR}/stdin" "${TEST_DIR}/out" "${TEST_DIR}/err" "${TEST_DIR}/function"
  echo "${1-0}" > "${TEST_DIR}/exit"
}

# args: case name
assert_wget_ran() {
  [ ! -f "${TEST_DIR}/function" ] || die "$1: expected the wget shell function to be bypassed"
  [ -f "${TEST_DIR}/args" ] || die "$1: expected the wget executable to run"
  [ "_$(cat "${TEST_DIR}/out")" = '_OUT' ] || die "$1: got stdout >$(cat "${TEST_DIR}/out")<, expected >OUT<"
  [ "_$(cat "${TEST_DIR}/err")" = '_ERR' ] || die "$1: got stderr >$(cat "${TEST_DIR}/err")<, expected >ERR<"
}

# given no URL with credentials
reset_logs 0
printf 'inherited\n' | nvm_wget_with_url_credentials -q -O - 'https://example.com/dist/index.tab' '' 'two words' > "${TEST_DIR}/out" 2> "${TEST_DIR}/err"
EXIT_CODE=$?
[ "_${EXIT_CODE}" = '_0' ] || die "no credentials: got exit code >${EXIT_CODE}<, expected >0<"
assert_wget_ran 'no credentials'
# then every argument is passed through as is, without --input-file
EXPECTED_ARGS='-q|
-O|
-|
https://example.com/dist/index.tab|
|
two words|'
[ "_$(cat "${TEST_DIR}/args")" = "_${EXPECTED_ARGS}" ] || die "no credentials: got args >$(cat "${TEST_DIR}/args")<, expected >${EXPECTED_ARGS}<"
# and wget reads the caller's stdin
[ "_$(cat "${TEST_DIR}/stdin")" = '_inherited' ] || die "no credentials: got stdin >$(cat "${TEST_DIR}/stdin")<, expected >inherited<"

# given no arguments at all
reset_logs 0
printf '' | nvm_wget_with_url_credentials > "${TEST_DIR}/out" 2> "${TEST_DIR}/err"
EXIT_CODE=$?
[ "_${EXIT_CODE}" = '_0' ] || die "no args: got exit code >${EXIT_CODE}<, expected >0<"
assert_wget_ran 'no args'
[ "_$(cat "${TEST_DIR}/args")" = '_' ] || die "no args: got args >$(cat "${TEST_DIR}/args")<, expected none"

# given wget fails, with no URL with credentials
reset_logs 8
printf '' | nvm_wget_with_url_credentials 'https://example.com/dist/index.tab' > "${TEST_DIR}/out" 2> "${TEST_DIR}/err"
EXIT_CODE=$?
# then its exit code is returned
[ "_${EXIT_CODE}" = '_8' ] || die "no credentials, wget fails: got exit code >${EXIT_CODE}<, expected >8<"
assert_wget_ran 'no credentials, wget fails'

# given one URL with credentials among other arguments
reset_logs 0
printf 'ignored\n' | nvm_wget_with_url_credentials -q 'https://user:p%40ss@example.com/dist/index.tab' -O 'out file' > "${TEST_DIR}/out" 2> "${TEST_DIR}/err"
EXIT_CODE=$?
[ "_${EXIT_CODE}" = '_0' ] || die "one credential URL: got exit code >${EXIT_CODE}<, expected >0<"
assert_wget_ran 'one credential URL'
# then the URL is removed from the arguments, the others keep their order, and wget reads URLs from stdin
EXPECTED_ARGS='-q|
-O|
out file|
--input-file=-|'
[ "_$(cat "${TEST_DIR}/args")" = "_${EXPECTED_ARGS}" ] || die "one credential URL: got args >$(cat "${TEST_DIR}/args")<, expected >${EXPECTED_ARGS}<"
# and the URL, newline-terminated, is the only thing on wget's stdin, in place of the caller's
STDIN="$(cat "${TEST_DIR}/stdin"; printf x)"
EXPECTED_STDIN='https://user:p%40ss@example.com/dist/index.tab
x'
[ "_${STDIN}" = "_${EXPECTED_STDIN}" ] || die "one credential URL: got stdin >${STDIN}<, expected >${EXPECTED_STDIN}<"
# and the credential is nowhere else
! grep -q 'p%40ss' "${TEST_DIR}/args" "${TEST_DIR}/out" "${TEST_DIR}/err" || die 'one credential URL: expected the credential only on stdin'

# given several URLs with credentials, one of them twice, and URLs without them
reset_logs 0
nvm_wget_with_url_credentials 'https://c:$HOME`id`\n\c@two.example/y' 'https://example.com/plain' 'http://a:b@one.example/x' 'https://example.com/at@path' 'http://a:b@one.example/x' < /dev/null > "${TEST_DIR}/out" 2> "${TEST_DIR}/err"
EXIT_CODE=$?
[ "_${EXIT_CODE}" = '_0' ] || die "several URLs: got exit code >${EXIT_CODE}<, expected >0<"
assert_wget_ran 'several URLs'
# then only the ones with credentials go to stdin, one per line, in order and verbatim
STDIN="$(cat "${TEST_DIR}/stdin"; printf x)"
EXPECTED_STDIN='https://c:$HOME`id`\n\c@two.example/y
http://a:b@one.example/x
http://a:b@one.example/x
x'
[ "_${STDIN}" = "_${EXPECTED_STDIN}" ] || die "several URLs: got stdin >${STDIN}<, expected >${EXPECTED_STDIN}<"
EXPECTED_ARGS='https://example.com/plain|
https://example.com/at@path|
--input-file=-|'
[ "_$(cat "${TEST_DIR}/args")" = "_${EXPECTED_ARGS}" ] || die "several URLs: got args >$(cat "${TEST_DIR}/args")<, expected >${EXPECTED_ARGS}<"

# given a URL with credentials that is the only argument
reset_logs 0
nvm_wget_with_url_credentials 'https://user:pass@example.com/' < /dev/null > "${TEST_DIR}/out" 2> "${TEST_DIR}/err"
EXIT_CODE=$?
[ "_${EXIT_CODE}" = '_0' ] || die "only a credential URL: got exit code >${EXIT_CODE}<, expected >0<"
assert_wget_ran 'only a credential URL'
[ "_$(cat "${TEST_DIR}/args")" = '_--input-file=-|' ] || die "only a credential URL: got args >$(cat "${TEST_DIR}/args")<, expected >--input-file=-|<"
[ "_$(cat "${TEST_DIR}/stdin")" = '_https://user:pass@example.com/' ] || die "only a credential URL: got stdin >$(cat "${TEST_DIR}/stdin")<, expected >https://user:pass@example.com/<"

# given wget fails, with a URL with credentials
reset_logs 4
nvm_wget_with_url_credentials 'https://user:pass@example.com/' < /dev/null > "${TEST_DIR}/out" 2> "${TEST_DIR}/err"
EXIT_CODE=$?
# then its exit code is returned
[ "_${EXIT_CODE}" = '_4' ] || die "credential URL, wget fails: got exit code >${EXIT_CODE}<, expected >4<"
assert_wget_ran 'credential URL, wget fails'

# given an argument with credentials and whitespace, which is not a valid URL
reset_logs 0
printf 'inherited\n' | nvm_wget_with_url_credentials 'https://user:pass@example.com/a b' > "${TEST_DIR}/out" 2> "${TEST_DIR}/err"
EXIT_CODE=$?
[ "_${EXIT_CODE}" = '_0' ] || die "whitespace: got exit code >${EXIT_CODE}<, expected >0<"
assert_wget_ran 'whitespace'
# then it stays an argument, and wget reads the caller's stdin
[ "_$(cat "${TEST_DIR}/args")" = '_https://user:pass@example.com/a b|' ] || die "whitespace: got args >$(cat "${TEST_DIR}/args")<, expected >https://user:pass@example.com/a b|<"
[ "_$(cat "${TEST_DIR}/stdin")" = '_inherited' ] || die "whitespace: got stdin >$(cat "${TEST_DIR}/stdin")<, expected >inherited<"

# given the caller has a variable with the name of its URL list
NVM_WGET_STDIN_URLS='https://x:y@caller.example/'
reset_logs 0
printf '' | nvm_wget_with_url_credentials 'https://example.com/' > "${TEST_DIR}/out" 2> "${TEST_DIR}/err"
# then the list starts empty, and the caller's value is kept
[ "_$(cat "${TEST_DIR}/args")" = '_https://example.com/|' ] || die "caller variable: got args >$(cat "${TEST_DIR}/args")<, expected >https://example.com/|<"
[ "_${NVM_WGET_STDIN_URLS}" = '_https://x:y@caller.example/' ] || die "caller variable: got >${NVM_WGET_STDIN_URLS}<, expected >https://x:y@caller.example/<"

# and its working variables do not reach the caller
unset NVM_WGET_STDIN_URLS NVM_WGET_STDIN_COUNT NVM_WGET_STDIN_ARG
reset_logs 0
nvm_wget_with_url_credentials 'https://user:pass@example.com/' -q < /dev/null > "${TEST_DIR}/out" 2> "${TEST_DIR}/err"
[ -z "${NVM_WGET_STDIN_URLS+set}${NVM_WGET_STDIN_COUNT+set}${NVM_WGET_STDIN_ARG+set}" ] || die 'expected the function locals to stay unset in the caller'

cleanup
